# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponse
from models import *
from django.utils import simplejson

def home(request):
	homes = Home.objects.all()
	return render_to_response("show.html",{'homes':homes})

def djynamyc_up(request):
	print "noway"
	results = {}
	print "que apsa"
	GET = request.GET
	action = GET['action']
	clas = GET['cls']
	clas = clas.capitalize()
	attr = GET['att']
	new_text = GET['info']
	ob_id = None
	if action == "update":
		id = GET['objid']
	
		exec("ret= %s.objects.get(id=%s)" % (clas,id))
		ret.content = new_text
		ret.save()
	elif action == "new":
		print clas,attr,new_text
		exec("ret = %s(%s='%s')" % (clas,attr,new_text))
		ret.save()
		ob_id = ret.id
		
	
	results['result']= new_text
	if ob_id:
		results['objidnew']= ob_id
	
	json = simplejson.dumps(results)	
	return HttpResponse(json, mimetype='application/json')